//index.js
//获取应用实例
const app = getApp();
var util = require('../../../../utils/util.js');
var dataArea
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userArea: app.globalData.Maintenance.userArea,
    allProvinceCity:null,
    provinceList: null,
    cityList: null,
    districtList: null,
    chooseProvince: null,
    areaProvince: true,
    buttonClicked: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    dataArea = options;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var _this = this
    wx.showLoading({
      title: '加载中',
    })
    // 获取所有省
    _this.setData({
      provinceList: app.globalData.allArea.result[0]
    })
    _this.cityList(dataArea.provinceId, dataArea.cityName);
    // 取消加载中
    wx.hideLoading();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  cityList(provinceId, cityName){
    var _this = this;
    var allCityList = app.globalData.allArea.result[1];
    var alldistrictList = app.globalData.allArea.result[2];

    var cityList = [];
    // 先添加一个
    var chooseProvince = {
      name: cityName,
      districtList: []
    }
    // 获得前3位代
    var proBef = provinceId.match(/^.{0,2}/)[0];
    // 以proBef 开头
    proBef = new RegExp("^"+proBef);
    // 去allProvinceCity找city
    for (var i=0; i < allCityList.length; i++){
      if (allCityList[i].id.search(proBef) != '-1'){
        cityList.push(allCityList[i]);
      }
    }
    console.log('cityList=', cityList);
    for (var a=0; a < cityList.length; a++){
      // 判断是否是直辖市 判断是否由两个零结尾
      if (cityList[a].id.search(/00$/g) != '-1'){
        // 是以两个零结尾的
        // 获得前4位代
        var cityBef = cityList[a].id.match(/^.{0,4}/)[0];
        // 以cityBef 开头
        cityBef = new RegExp("^"+cityBef);
        // 去alldistrictList 找区
        var districtList = []
        for (var b = 0; b < alldistrictList.length; b++) {
          if (alldistrictList[b].id.search(cityBef) != '-1') {
            districtList.push(alldistrictList[b]);
          }
        }
        cityList[a]["districtList"] = districtList;
      }else{
        chooseProvince.districtList.push(cityList[a]);
      }
    }
    if (chooseProvince.districtList.length && chooseProvince.districtList.length < cityList.length){
      chooseProvince.name = '其他'
    }
    _this.setData({
      cityList: cityList,
      chooseProvince: chooseProvince,
      areaProvince: false,
      userArea: {
        province: cityName,
        city:'',
        district:'',
        adcode:''
      }
    })
    console.log('city', _this.data.cityList);
    console.log('chooseProvince', _this.data.chooseProvince)
  },
  chooseArea(e){
    var _this = this;
    // 获取city district
    var city = e.target.dataset.city ? e.target.dataset.city : e.currentTarget.dataset.city;
    var district = e.target.dataset.district ? e.target.dataset.district : e.currentTarget.dataset.district;
    var adcode = e.target.dataset.id ? e.target.dataset.id : e.currentTarget.dataset.id;
    // 获取地址
    // 省
    var userArea = app.globalData.Maintenance.userArea;
    if (userArea){
      userArea.province = _this.data.userArea.province,
      userArea.city = city,
      userArea.district = district,
      userArea.adcode = adcode
    }else{
      userArea = {}
      userArea.province = _this.data.userArea.province,
      userArea.city = city,
      userArea.district = district,
      userArea.adcode = adcode
    }
    
    // 修改地址
    app.globalData.Maintenance.userArea = userArea;
    // 返回上一个页面
    var pages = getCurrentPages();
    var prevPage = pages[0];  //上一个页面
    prevPage.setData({
      userArea: userArea,
    })
    prevPage.popConnectionOpen();
    if (!_this.data.buttonClicked) {
      util.buttonClicked(_this);
      wx.navigateBack({
        delta: pages.length - 1
      })
    }
  }
})

